﻿using UnityEngine;
using System;
using System.Collections;

using Hont;

public class _ATAsync_MonoTestExample : MonoBehaviour
{
    void Start()
    {
        var asyncOp = new ATAsync();
        var intValue = asyncOp.Await<int>(ReturnInt());

        asyncOp.Async(() =>
        {
            if (intValue.Value > 20)
            {
                asyncOp.Await(Wait1Sec());
                asyncOp.Async(() =>
                {
                    Debug.Log("End! " + Time.time);
                });
            }
        });

        Debug.Log("Strart Function Execute Finished !" + Time.time);
    }

    IATTask ReturnInt()
    {
        var result = new ATTask<int>(handle =>
        {
            StartCoroutine(Wait(1f, () => handle.Finished(21)));
        });

        return result;
    }

    IATTask Wait1Sec()
    {
        var result = new ATTask(handle =>
        {
            Debug.Log("Wait1Sec Function Execute! " + Time.time);
        });

        StartCoroutine(Wait(1f, () => result.Finished()));

        return result;
    }

    IEnumerator Wait(float time, Action content)
    {
        yield return new WaitForSeconds(time);

        content();
    }
}
